package aircraftWar;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * 说明：天空对象
 *
 * @author: PYS
 * @date: 2022/11/10
 **/
public abstract class FlyingObject {
    protected static final int LIVE = 0;
    protected static final int DEAD = 1;
    protected static final int REMOVE = 2;
    protected int state = LIVE;

    protected int width;
    protected int height;
    protected int x;
    protected int y;
    protected int speed;

    //三种敌机构造方法
    public FlyingObject(int width, int height) {
        this.width = width;
        this.height = height;
        Random rand = new Random();
        x = rand.nextInt(World.WIDTH-width+1);
        y = -height;
        speed = 2;
    }

    //英雄机，天空， 子弹构造方法
    public FlyingObject(int width, int height, int x, int y) {
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
    }

    /** 飞行物移动 */
    public abstract void step();

    /** 获取对象的图片 */
    public abstract BufferedImage getImage();

    /** 判断对象是否是活着的 */
    public boolean isLive() {
        return state == LIVE;
    }

    /** 判断对象是否是死了的 */
    public boolean isDead() {
        return state == DEAD;
    }

    /** 判断对象是否是删除的 */
    public boolean isRemove() {
        return state == REMOVE;
    }

    public boolean isOutOfBounds() {
        return this.y >= World.HEIGHT;
    }

    public boolean isHit(FlyingObject other) {
        int x1 = this.x-other.width;  //x1:敌人的x-子弹/英雄机的宽
        int x2 = this.x+this.width;   //x2:敌人的x+敌人的宽
        int y1 = this.y-other.height; //y1:敌人的y-子弹/英雄机的高
        int y2 = this.y+this.height;  //y2:敌人的y+敌人的高
        int x = other.x;              //x:子弹/英雄机的x
        int y = other.y;              //y:子弹/英雄机的y

        return x>=x1 && x<=x2 && y>=y1 && y<=y2;
    }

    public void goDead() {
        this.state = DEAD;
    }
}
